package com.augurit.gzcgwsp.excelimportexport.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;
public class GetFileName {
	
	public static String getFileName(String fileExt) {
	    String fileName;
	    Double ran = new Double(98 * Math.random() + 1);
	    Random random = new Random(); 
	    int rannum = random.nextInt(9999999); 
	    String formatDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	    fileName =formatDate+""+rannum;
	    fileName = fileName + "." + fileExt;
	    return fileName;
	  }
	
	
    /**
     * 生成后面的文件路径加上年月
     * @param realpath 定义的目录
     * @param fileNamesuffix 文件后缀名
     * @return
     */
    public static String GetRealPath(String realpath,String fileNamesuffix){
        String fileName = GetFileName.getFileName(fileNamesuffix);
        String dateDir = GetFileName.getDirector();
        return realpath+"/"+dateDir+"/"+fileName;
        
    }
    
    
	  /**
	   * 以年和月份作为上传文件的文件夹名称
	   * @return String
	   */
	  public static String getDirector(){
	    Date myDate = new Date();
	    String director = myDate.getYear() + 1900 + "" + (myDate.getMonth() + 1);
	    return director;
	  }
	  
	  /**
	   * 生成存放路径
	   * @param fileExt
	   * @param parentPath
	   * @return
	   */
	  public static String getExcelPath(String fileExt,String parentPath){
		  String fileName = GetFileName.getFileName(fileExt);
		  String director = GetFileName.getDirector();
		  String nodeStr = System.getProperty("file.separator");  
		  File root = new File(parentPath+nodeStr+director);
		  if(!root.exists()){
			  root.mkdirs();
		  }
		  
		  return root.getAbsolutePath()+nodeStr+fileName;
	  }
	  
	  /**
       * 生成存放路径
       * @param fileExt
       * @param parentPath
       * @return
       */
	   public static String getFileName(String fileExt,String parentPath){
	          String fileName = GetFileName.getFileName(fileExt);
	          String director = GetFileName.getDirector();
	          String nodeStr = System.getProperty("file.separator");  
	          File root = new File(parentPath+nodeStr+director);
	          if(!root.exists()){
	              root.mkdir();
	          }
	          return fileName;
	      }
	  
	  /**
	   * 创建文件
	   * @param filepath
	   * @return
	   */
	  public static File createFile(String filepath){	
	      File file=null;
	      try {
            String dir=filepath.substring(0,filepath.lastIndexOf(File.separator));
              File root=new File(dir);
              if(!root.exists()){
                  root.mkdir();
              }
              file=new File(filepath);
              if(!file.exists()){
                    file.createNewFile();
              }
              
        } catch (Exception e) {
            e.printStackTrace();
        }
	      return file;
	  }
	  
	  /**
	   *   随机生成文件名后缀
	   * @return
	   */
	  public static String buildRomFileName(String suffix){
	        String formatDate = new SimpleDateFormat("yyyyMMddHHmmss")
	                .format(new Date());
	        int random = new Random().nextInt(10000);
	        return  formatDate + random+"."+suffix;
	  }
	  
	  /**
	   * 下载Excel 
	   * @param filePath  服务器,统计完后的Excel 路径
	   * @param response  
	   * @param outPutName  下载客户端提标重命名,名称
	   */
	  
	  public void downLoadExcel(String filePath, HttpServletResponse response,
			String outPutName) {
		String filename = outPutName + ".xls";
		try {
			// 别的输出方式
			// response.addHeader("Content-Disposition", "inline;filename=" +
			// new String( filename.getBytes("gbk"), "ISO8859-1"
			// ));//"attachment;filename=" + new String(
			// filename.getBytes("gbk"), "ISO8859-1" ) );
			// response.setContentType("application/vnd.ms-excel;charset=GBK");//application/ms-excel
			InputStream inputStream = new FileInputStream(filePath);
			response.addHeader("Content-Disposition", "attachment;filename="
					+ new String(filename.getBytes("gbk"), "ISO8859-1"));
			response.setContentType("application/ms-excel;charset=GBK");
			OutputStream outputStream = response.getOutputStream();
			byte[] buffer = new byte[1024];
			int i = -1;
			while ((i = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, i);
			}
			outputStream.flush();
			outputStream.close();
			inputStream.close();
			outputStream = null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	  
	  
	  
	  public static void main(String[] arg,HttpServletResponse response){
		  
		  System.out.print(getExcelPath("xls","f:\\test"));
	  }
}
